//
//  WCNEffectServiceTimeControll.m
//  WCNEffectServiceTimeVC
//
//  Created by wsj2015 on 15/12/14.
//  Copyright © 2015年 wsj2015. All rights reserved.
//

#import "WCNEffectServiceTimeControll.h"
#import "WCNDayCell.h"
#import "WSJAnimationTool.h"
#import "WCNetworkAccessManager.h"
#import "Macros.h"

@interface WCNEffectServiceTimeControll ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic ,retain)NSMutableArray *dataArray;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableViewWidth;

@end

@implementation WCNEffectServiceTimeControll
{
    NSInteger _selecetIndex;
    NSInteger _selectBtn;
}
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray =[NSMutableArray array];
    }
    return _dataArray;
}
-(void)awakeFromNib
{
    _selecetIndex = 0;
    _selectBtn = 200;
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"Mult_Select"] forState:UIControlStateSelected];
    [self.leftBtn setBackgroundImage:[UIImage imageNamed:@"Selected_no"] forState:UIControlStateNormal];
    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"Mult_Select"] forState:UIControlStateSelected];
    [self.rightBtn setBackgroundImage:[UIImage imageNamed:@"Selected_no"] forState:UIControlStateNormal];
    self.leftBtn.selected = YES;
    self.tableViewHeight.constant =self.frame.size.width;
    self.tableViewWidth.constant =59;
    self.tableView.transform  =CGAffineTransformMakeRotation(-M_PI / 2);
    [self.leftBtn addTarget:self action:@selector(timeTypeChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.rightBtn addTarget:self action:@selector(timeTypeChooseAction:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.contentInset = UIEdgeInsetsMake(40, 0, 40, 0);
    
    //请求数据
    [self getMonthTime];
    

}
- (void)timeTypeChooseAction:(UIButton *)btn
{
    UIButton *preBtn =(UIButton *)[self viewWithTag:_selectBtn];
    if (btn == preBtn) {
        return;
    }
    [WSJAnimationTool animationEaseIn:btn];
    preBtn.selected = NO;
    btn.selected = YES;
    _selectBtn = btn.tag;
    if (self.tiemTypeSlectBolck) {
        WCNEfffectServiceTimeType type;
        if (_selectBtn ==200) {
            type =WCNEfffectServiceTimeTypeHospitalIn;
        }else{
            type=WCNEfffectServiceTimeTypeHospitalOut;
        }
        self.tiemTypeSlectBolck(type);
    }
    

}
- (void)getMonthTime
{
    weakSelf_define;
    [[WCNetworkAccessManager sharedMg] GET:kCurrentMonthUrl_part parameters:nil success:^(NSDictionary *responseObject) {
        for (NSDictionary *dict in responseObject[@"data"]) {
            WCNYearDate *data =[[WCNYearDate alloc] init];
            [data setValuesForKeysWithDictionary:dict];
            [weakSelf.dataArray addObject:data];
        }
        [weakSelf.tableView reloadData];
        if (weakSelf.lodingFinish) {
            weakSelf.lodingFinish(weakSelf.dataArray.firstObject);
        }
        
    } failure:nil netError:nil always:^{
        
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%i",self.dataArray.count);
    return  self.dataArray.count;
   
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCNDayCell *cell = [WCNDayCell cellForTabelView:tableView];
    
    cell.dateModel =self.dataArray[indexPath.row];
    
    if (indexPath.row ==0) {
        [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:0];
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row ==_selecetIndex) {
        return;
    }
    _selecetIndex = indexPath.row;
    if (self.dateBlock) {
        self.dateBlock(self.dataArray[_selecetIndex]);
    }
    
 
}
+ (instancetype)viewFromNib
{
    return [[[NSBundle mainBundle] loadNibNamed:@"WCNEffectServiceTimeControll" owner:nil options:0]lastObject];
 
}

@end
